//
//  WKSubmitVideoCell.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/19.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKSubmitVideoCell.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>


@interface WKSubmitVideoCell ()
@property(nonatomic,strong)UIImageView *addvideoImgView;
@property(nonatomic,strong)UIImageView *videobgView;
@property(nonatomic,strong)UIButton *palyvideoBtn;
@property(nonatomic,strong)UILabel *videoTimeLb;
@property(nonatomic,strong)NSArray *imgs;
@property(nonatomic,strong)NSArray *assets;
@end

@implementation WKSubmitVideoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if ([reuseIdentifier isEqualToString:@"VIDEOADDVIDEO"]) {
            [self addvideoView];
        }
        if ([reuseIdentifier isEqualToString:@"VIDEOTAGVIEW"]) {
            [self setupVideoTagView];
        }
        
        if ([reuseIdentifier isEqualToString:@"VIDEOCONTENVIEW"]) {
            [self setupVideocontentView];
        }
    }
    return self;
}

#pragma mark - 添加视频
- (void)addvideoView{
    
    self.addvideoImgView = [[UIImageView alloc]init];
    self.videobgView = [[UIImageView alloc]init];
    self.palyvideoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.videoTimeLb = [[UILabel alloc]init];
    _videoTimeLb.textColor = [UIColor whiteColor];
    _videoTimeLb.font = PFSC_RegularFont(11);
    
    _addvideoImgView.userInteractionEnabled = YES;
    _videobgView.userInteractionEnabled = YES;
    
    [self.contentView addSubview:self.addvideoImgView];
    [self.contentView addSubview:self.videobgView];
    [self.videobgView addSubview:self.palyvideoBtn];
    [self.videobgView addSubview:self.videoTimeLb];
    
    self.addvideoImgView.backgroundColor = [UIColor yellowColor];
    
    self.videobgView.backgroundColor = [UIColor cyanColor];
    
    self.palyvideoBtn.backgroundColor = BACK_COLOR;
    
    [_palyvideoBtn addTarget:self action:@selector(playAtion) forControlEvents:UIControlEventTouchUpInside];
    
    [self.addvideoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(114, 114));
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
    }];
    
    [self.videobgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(12);
        make.top.mas_offset(15);
        make.right.mas_offset(-12);
        make.bottom.mas_offset(-15);
    }];

    [self.palyvideoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.center.mas_equalTo(self.videobgView.center);
    }];

    [self.videoTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(65, 10));
        make.right.mas_equalTo(-12);
        make.bottom.mas_equalTo(-15);
    }];
    
    _videobgView.contentMode = UIViewContentModeScaleAspectFill;
    _videobgView.layer.cornerRadius = 5;
    _videobgView.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addvideo)];
    [_addvideoImgView addGestureRecognizer:tap];
    _videobgView.hidden = YES;
}

- (void)addvideo{
    
    ZLPhotoActionSheet *ac = [[ZLPhotoActionSheet alloc] init];
    ac.configuration.maxSelectCount = 1;
    ac.configuration.maxPreviewCount = 10;
    ac.configuration.maxVideoDuration = 10;
    ac.sender = [WHUtiities currentViewController];
    ac.configuration.allowSelectVideo = YES;
    ac.configuration.allowSelectImage = NO;
    ac.configuration.allowEditVideo = YES;
    [ac showPreviewAnimated:YES];
    
    @weakify(self);
    [ac setSelectImageBlock:^(NSArray<UIImage *> * _Nullable images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        @strongify(self);
        
        UIImage *img = [images lastObject];
        
        self.imgs = images;
        self.assets = assets;
        
        self.videobgView.image = img;
        
        self.videobgView.hidden = NO;
        
        self.addvideoImgView.hidden = YES;
        
        PHAsset *lastsset =  [assets lastObject];
        CGFloat width  = (CGFloat)lastsset.pixelWidth;
        CGFloat height = (CGFloat)lastsset.pixelHeight;
        CGSize size = CGSizeMake(width, height);
        //按照尺寸大小判断是横竖屏
    
        
        
    }];
    
}

- (void)playAtion{
    ZLPhotoActionSheet *ac = [[ZLPhotoActionSheet alloc] init];
    ac.sender = [WHUtiities currentViewController];
    [ac previewSelectedPhotos:self.imgs assets:self.assets index:0 isOriginal:YES];
}

#pragma mark - 添加标签
- (void)setupVideoTagView{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(12, 8, 70, 30)];
    lable.textColor = UIColorFromHex(0x3C3C3C);
    lable.font = PFSC_MediumFont(15);
    NSString *tagStr = @"*添加标签";
    [WHAttributedString setRichTextOnlyColor:lable titleString:tagStr textColor:UIColorFromHex(0xF54050) colorRang:NSMakeRange(0, 1)];
    [self.contentView addSubview:lable];
    
    UILabel *waingLb = [[UILabel alloc]initWithFrame:CGRectMake(90, 8, ScreenWidth-90-12-17-5, 30)];
    waingLb.backgroundColor = [UIColor yellowColor];
    waingLb.textColor = UIColorFromHex(0x979797);
    waingLb.font = PFSC_MediumFont(12);
    [self.contentView addSubview:waingLb];
    
    UIButton* addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton addTarget:self action:@selector(addtagAction) forControlEvents:UIControlEventTouchUpInside];
    addButton.frame = CGRectMake(ScreenWidth-17-12, 13,17, 17);
    addButton.backgroundColor = [UIColor greenColor];
    addButton.layer.cornerRadius = 17/2;
    addButton.layer.masksToBounds = YES;
    [self.contentView addSubview:addButton];
}

#pragma mark - 添加标签
- (void)addtagAction{
    NSLog(@"添加标签");
}

#pragma mark - 输入内容
- (void)setupVideocontentView{
    
    self.contentView.backgroundColor = UIColorFromHex(0xF8F8F8);
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 0, ScreenWidth-24, 130)];
    textView.placeholder = @"想美~就要大胆问出来";
    textView.textColor = UIColorFromHex(0x3C3C3C);
    textView.font = PFSC_MediumFont(15);
    [self.contentView addSubview:textView];
    
    UILabel *waingLb = [[UILabel alloc]init];
    waingLb.backgroundColor = [UIColor yellowColor];
    waingLb.textColor = UIColorFromHex(0x979797);
    waingLb.font = PFSC_MediumFont(12);
    waingLb.text = @"0/140";
    waingLb.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:waingLb];
    [waingLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-12);
        make.bottom.mas_offset(-15);
        make.width.mas_offset(60);
    }];
    
    
    [[textView rac_textSignal]subscribeNext:^(NSString * _Nullable x) {
        NSInteger yslength = x.length;
        NSInteger sylength = 140 - yslength;
        waingLb.text = [NSString stringWithFormat:@"%ld/%ld",yslength,sylength];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
